<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>神奇字符串</title>
</head>
<body>
  <script>
    function fn(n) {
      let s = [1, 2, 2];
      for (let i = 2; s.length < n; i++) {
        let last = s[s.length - 1];
        for (let j = 0; j < s[i]; j++) {
          // 将 last 和 3 转换为二进制，数字进行相等，相等的为0（0相等为0），不相等的为1，结果再转为十进制
          // const a = 5;        // 00000000000000000000000000000101
          // const b = 3;        // 00000000000000000000000000000011
          // console.log(a ^ b); // 00000000000000000000000000000110 // 6
          s.push(last ^ 3);
        }
      }
      return s.slice(0, n).filter(x => x === 1).length;
    }
    console.log(fn(6))
  </script>
</body>
</html>